package com.example.tedtalksimproved;

import com.example.tedtalksimproved.adapter.VideoListAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;


/**
 * An activity representing a list of Videos. This activity has different
 * presentations for handset and tablet-size devices. On handsets, the activity
 * presents a list of items, which when touched, lead to a
 * {@link VideoDetailActivity} representing item details. On tablets, the
 * activity presents the list of items and item details side-by-side using two
 * vertical panes.
 * <p>
 * The activity makes heavy use of fragments. The list of items is a
 * {@link VideoListFragment} and the item details (if present) is a
 * {@link VideoDetailFragment}.
 * <p>
 * This activity also implements the required
 * {@link VideoListFragment.Callbacks} interface to listen for item selections.
 */
public class VideoListActivity extends FragmentActivity implements
		VideoListFragment.Callbacks{

	private VideoListAdapter mAdapter;
	private static boolean activeView = true;
	
	/**
	 * Whether or not the activity is in two-pane mode, i.e. running on a tablet
	 * device.
	 */
	private static boolean mTwoPane = false;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(activeView){
			View detailsFrame = findViewById(R.id.video_detail_container);
			if(detailsFrame!=null) detailsFrame.setVisibility(View.VISIBLE);
			mTwoPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
			if(mTwoPane) setContentView(R.layout.activity_video_twopane);
			else setContentView(R.layout.activity_video_list);
			VideoListFragment fragment = new VideoListFragment();
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fragment_content, fragment).commit();
		}else{
			 Log.i("Grid mode", "Should reach grid mode");
			setContentView(R.layout.activity_video_list);
			VideoGridFragment grid_fragment = new VideoGridFragment();
    		getSupportFragmentManager().beginTransaction()
    				.replace(R.id.fragment_content, grid_fragment).commit();
			FrameLayout layout = (FrameLayout)findViewById(R.id.video_detail_container);
			if(layout!=null) layout.setVisibility(View.GONE);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    // Inflate the menu items for use in the action bar
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.actions, menu);
	    return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle presses on the action bar items
	    switch (item.getItemId()) {
	        case R.id.action_favorites:
	            Log.i("Favorites clicked", "Favorites clicked");
	            new VideoListFragment().openFavorites();
	            /// need to access Adapter 
	            return true;
	        case R.id.action_home:
	            Log.i("Home clicked", "Home clicked");
	            FrameLayout detail_frame2 = (FrameLayout)findViewById(R.id.video_detail_container);
	            if(detail_frame2!=null)detail_frame2.setVisibility(View.VISIBLE);;
	            //new VideoListFragment().openHome();
	            VideoListFragment list_fragment = new VideoListFragment();
	    		getSupportFragmentManager().beginTransaction()
	    				.replace(R.id.fragment_content, list_fragment).commit();
	    		activeView = true;
	            return true;
	        case R.id.action_grid:
	            Log.i("Home clicked", "Grid clicked");
	            //Intent gridIntent = new Intent(this, GridActivity.class);
				//startActivity(gridIntent);
	            //new VideoListFragment().openGrid();
	            activeView = false;
	            FrameLayout detail_frame = (FrameLayout)findViewById(R.id.video_detail_container);
	            if(detail_frame!=null) detail_frame.setVisibility(View.GONE);
	            VideoGridFragment grid_fragment = new VideoGridFragment();
	    		getSupportFragmentManager().beginTransaction()
	    				.replace(R.id.fragment_content, grid_fragment).commit();
	            
	            
	            return true;  
	        default:

	    return super.onOptionsItemSelected(item);
	    }	    
	}
	/**
	 * Callback method from {@link VideoListFragment.Callbacks} indicating that
	 * the item with the given ID was selected.
	 */
	@Override
	public void onItemSelected(String id) {
		View detailsFrame = findViewById(R.id.video_detail_container);
		if (detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE) {
			Log.i("Two pane", "TWO PANE");
			// In two-pane mode, show the detail view in this activity by
			// adding or replacing the detail fragment using a
			// fragment transaction.
			Bundle arguments = new Bundle();
			arguments.putString(VideoDetailFragment.ARG_ITEM_ID, id);
			VideoDetailFragment fragment = new VideoDetailFragment();
			fragment.setArguments(arguments);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.video_detail_container, fragment).commit();

		} else {
			// In single-pane mode, simply start the detail activity
			// for the selected item ID.
			Intent detailIntent = new Intent(this, VideoDetailActivity.class);
			detailIntent.putExtra(VideoDetailFragment.ARG_ITEM_ID, id);
			startActivity(detailIntent);
		}
	}
}
